/**
 *  文件名:BirthdayActivity.java
 *  修改人:lihangjie
 *  创建时间:2015-9-8 上午9:16:20
 */
package com.sharemob.tinchat.modules.account;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sharemob.tinchat.R;
import com.sharemob.tinchat.base.BaseActivity;
import com.sharemob.tinchat.lib.common.SMGlobal;
import com.sharemob.tinchat.lib.datepicker.NumericWheelAdapter;
import com.sharemob.tinchat.lib.datepicker.OnWheelScrollListener;
import com.sharemob.tinchat.lib.datepicker.WheelView;
import com.sharemob.tinchat.modules.dao.cache.CacheManager;

/**
 * 
 * <一句话功能简述>
 * 
 * @author lihangjie version [版本号,2015-9-8 上午9:16:20]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 * 
 */
public class BirthdayActivity extends BaseActivity {


	private LayoutInflater inflater = null;
	private WheelView year;
	private WheelView month;
	private WheelView day;
	private int mYear=1996;
	private int mMonth=0;
	private int mDay=1;

	LinearLayout ll;
	TextView tv_account_age,tv_account_constellation;
	

	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_birthday);
		
		inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
		ll=(LinearLayout) findViewById(R.id.ll);
		ll.addView(getDataPick());
		tv_account_age=(TextView) findViewById(R.id.tv_account_age);
		tv_account_age.setText(String.valueOf(CacheManager.getInstance().getAccount().getAge())+"岁");
		
		tv_account_constellation=(TextView) findViewById(R.id.tv_account_constellation);
		tv_account_constellation.setText(CacheManager.getInstance().getAccount().getConstellation());
		
		exitView(R.id.title_back, R.anim.out_from_top,0 );
 
	}

	protected void keyEventOfListView(int position, int id) {

	}
	
	private View getDataPick() {
		Calendar c = Calendar.getInstance();
		int norYear = c.get(Calendar.YEAR);
//		int curMonth = c.get(Calendar.MONTH) + 1;//通过Calendar算出的月数要+1
//		int curDate = c.get(Calendar.DATE);
		
		int curYear = mYear;
		int curMonth =mMonth+1;
		int curDate = mDay;
		
		final View view = inflater.inflate(R.layout.layout_wheel_datepicker, null);
		
		year = (WheelView) view.findViewById(R.id.year);
		year.setAdapter(new NumericWheelAdapter(1950, norYear));
		year.setLabel("年");
		year.setCyclic(true);
		year.addScrollingListener(scrollListener);
		
		month = (WheelView) view.findViewById(R.id.month);
		month.setAdapter(new NumericWheelAdapter(1, 12, "%02d"));
		month.setLabel("月");
		month.setCyclic(true);
		month.addScrollingListener(scrollListener);
		
		day = (WheelView) view.findViewById(R.id.day);
		initDay(curYear,curMonth);
		day.setLabel("日");
		day.setCyclic(true);

		
		try {
			Calendar cal = Calendar.getInstance();
			SimpleDateFormat formate=new SimpleDateFormat("yyyy-MM-dd") ;
			Date ageDate=formate.parse(CacheManager.getInstance().getAccount().getBirthday());
			cal.setTime(ageDate);
			
			int ageYear=cal.get(Calendar.YEAR);
			year.setCurrentItem(ageYear-1950);
			
			int ageMonth=cal.get(Calendar.MONTH);
			month.setCurrentItem(ageMonth);
			
			int ageDay=cal.get(Calendar.DATE)-1;
			day.setCurrentItem(ageDay);
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		System.out.println(ageDate.getYear());
		
//		year.setCurrentItem(curYear - 1950);
//		year.setCurrentItem(40);
//		month.setCurrentItem(curMonth - 1);
//		day.setCurrentItem(curDate - 1);
		
//		Button bt = (Button) view.findViewById(R.id.set);
//		bt.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
////				birthday = (year.getCurrentItem()+1950) + "-"+ (month.getCurrentItem()+1)+"-"+(day.getCurrentItem()+1);
//				String birthday=new StringBuilder().append((year.getCurrentItem()+1950)).append("-").append((month.getCurrentItem() + 1) < 10 ? "0" + (month.getCurrentItem() + 1) : (month.getCurrentItem() + 1)).append("-").append(((day.getCurrentItem()+1) < 10) ? "0" + (day.getCurrentItem()+1) : (day.getCurrentItem()+1)).toString();
//		        Toast.makeText(getApplicationContext(), birthday, 1).show();
//			}
//		});
//		Button cancel = (Button) view.findViewById(R.id.cancel);
//		cancel.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				if(null!=menuWindow){
//					menuWindow.dismiss();
//				}
//			}
//		});
		return view;
	}
	
	OnWheelScrollListener scrollListener = new OnWheelScrollListener() {
		@Override
		public void onScrollingStarted(WheelView wheel) {

		}

		@Override
		public void onScrollingFinished(WheelView wheel) {
			int n_year = year.getCurrentItem() + 1950;//
			int n_month = month.getCurrentItem() + 1;//
			initDay(n_year,n_month);
			String birthday=new StringBuilder().append((year.getCurrentItem()+1950)).append("-").append((month.getCurrentItem() + 1) < 10 ? "0" + (month.getCurrentItem() + 1) : (month.getCurrentItem() + 1)).append("-").append(((day.getCurrentItem()+1) < 10) ? "0" + (day.getCurrentItem()+1) : (day.getCurrentItem()+1)).toString();
			
			String age=calculateDatePoor(birthday);
			tv_account_age.setText(age+"岁");
			CacheManager.getInstance().getAccount().setAge(Integer.parseInt(age));
			CacheManager.getInstance().getAccount().setBirthday(birthday);
			
			String constellation=SMGlobal.getConstellation(n_month, day.getCurrentItem()+1);
			tv_account_constellation.setText(constellation);
			CacheManager.getInstance().getAccount().setConstellation(constellation);
			
			//发送更新数据
			Message msg=new Message();
			msg.what=ActivityAccountInfo.KeyBirthday;
			Bundle bundleData = new Bundle();
			bundleData.putString("birthday",birthday);
			bundleData.putString("constellation",constellation);
			msg.setData(bundleData);
			SMGlobal.getInstance().handler.sendMessage(msg);
		}
	};

	/**
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	private int getDay(int year, int month) {
		int day = 30;
		boolean flag = false;
		switch (year % 4) {
		case 0:
			flag = true;
			break;
		default:
			flag = false;
			break;
		}
		switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			day = 31;
			break;
		case 2:
			day = flag ? 29 : 28;
			break;
		default:
			day = 30;
			break;
		}
		return day;
	}

	/**
	 */
	private void initDay(int arg1, int arg2) {
		day.setAdapter(new NumericWheelAdapter(1, getDay(arg1, arg2), "%02d"));
	}
	public static final String calculateDatePoor(String birthday) {
		try {
			if (TextUtils.isEmpty(birthday))
				return "0";
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date birthdayDate = sdf.parse(birthday);
			String currTimeStr = sdf.format(new Date());
			Date currDate = sdf.parse(currTimeStr);
			if (birthdayDate.getTime() > currDate.getTime()) {
				return "0";
			}
			long age = (currDate.getTime() - birthdayDate.getTime())
					/ (24 * 60 * 60 * 1000) + 1;
			String year = new DecimalFormat("0.00").format(age / 365f);
			if (TextUtils.isEmpty(year))
				return "0";
			return String.valueOf(new Double(year).intValue());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "0";
	}
}
